package net.yla.smartbus.module.ba.smsMessages.serivce.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import net.yla.smartbus.core.api.RequestCode;
import net.yla.smartbus.core.base.BaseService;
import net.yla.smartbus.core.exception.AppException;
import net.yla.smartbus.core.util.DtoUtils;
import net.yla.smartbus.module.ba.smsMessages.dto.SmsMessagesDTO;
import net.yla.smartbus.module.ba.smsMessages.entity.SmsMessagesDO;
import net.yla.smartbus.module.ba.smsMessages.form.SmsMessagesForm;
import net.yla.smartbus.module.ba.smsMessages.mapper.SmsMessagesDOMapper;
import net.yla.smartbus.module.ba.smsMessages.query.CaptchaQuery;
import net.yla.smartbus.module.ba.smsMessages.query.SmsMessagesQuery;
import net.yla.smartbus.module.ba.smsMessages.serivce.SmsMessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author:LiuJie
 * @date:2020/8/17
 * @description:
 */
@Service
public class SmsMessagesServiceImpl extends BaseService implements SmsMessagesService {

    @Autowired
    private SmsMessagesDOMapper smsMessagesDOMapper;

    /**
     * 新增短信发送日志
     *
     * @param record
     * @return
     */
    @Override
    public int add(SmsMessagesDO record) {
        if (record.getTo() == null || record.getFrom() == null || record.getMessage() == null || record.getCount() == null
                || record.getIp() == null || record.getMessageType() == null || record.getState() == null) {
            throw new AppException("to,from,message,count,ip,messageType,state不能为空", 410);
        }
        smsMessagesDOMapper.insertSelective(record);
        return 1;
    }

    /**
     * 通过form表单发送短信日志
     *
     * @param form
     * @return
     */
    @Override
    public int addForm(SmsMessagesForm form) {
        SmsMessagesDO record = new SmsMessagesDO();
        //目标手机号
        record.setTo(form.getTo());
        //发送方号码
        record.setFrom(form.getFrom());
        //短信内容
        record.setMessage(form.getMessage());
        //实际发送短信数据
        record.setCount(form.getCount());
        //客户端地址
        record.setIp(form.getIp());
        //短信类别
        record.setMessageType(form.getMessageType());
        //发送状态
        record.setState(form.getState());
        //短信平台
        record.setSmsPlatform(form.getSmsPlatform());
        int i = this.add(record);
        if (i < 1) {
            throw new AppException("添加短信日志失败", RequestCode.forbid_error);
        }
        return 1;
    }

    /**
     * 查看已发送的信息
     *
     * @param smsId
     * @return
     */
    @Override
    public JSONObject selectByPrimaryKey(Integer smsId) {
        SmsMessagesDO smsMessagesDO = smsMessagesDOMapper.selectByPrimaryKey(smsId);
        JSONObject jsonObject = null;
        if (smsMessagesDO != null) {
            jsonObject = DtoUtils.asDtoJsonForOnly(smsMessagesDO);
        }
        return jsonObject;
    }

    /**
     * 查看发送总数
     *
     * @param query
     * @return
     */
    @Override
    public Integer selectSendSmsCount(SmsMessagesQuery query) {
        return smsMessagesDOMapper.selectSendSmsCount(query);
    }

    /**
     * 查看已发送信息
     *
     * @param query
     * @return
     */
    @Override
    public List<JSONObject> listSendSmsMessages(SmsMessagesQuery query) {
        List<SmsMessagesDO> smsMessagesDOS = smsMessagesDOMapper.listSendSmsMessages(query);
        List<JSONObject> jsonObjects = new LinkedList<>();
        JSONObject jsonObject = null;
        if (smsMessagesDOS != null && smsMessagesDOS.size() != 0) {
            for (SmsMessagesDO smsMessagesDO : smsMessagesDOS) {
                jsonObject = DtoUtils.asDtoJsonForOnly(smsMessagesDO);
                jsonObjects.add(jsonObject);
            }
        }
        return jsonObjects;
    }

    /**
     * 清理日志
     *
     * @param query
     * @return
     */
    @Override
    public int cleanSmsMessages(SmsMessagesQuery query) {
        return smsMessagesDOMapper.cleanSmsMessages(query);
    }

    /**
     * 清理验证码
     *
     * @param query
     * @return
     */
    @Override
    public int cleanCaptcha(CaptchaQuery query) {
        return smsMessagesDOMapper.cleanCaptcha(query);
    }


}
